package com.ql.controller.zydj;

import com.alibaba.fastjson.JSON;
import com.ql.pojo.bed.Bed;
import com.ql.pojo.user.Keshi;
import com.ql.pojo.yk.Doctor;
import com.ql.pojo.zydj.Zuyuandengji;
import com.ql.pojo.zyyz.Zuyuanyz;
import com.ql.service.bedService.BedService;
import com.ql.service.doctor.DoctorService;
import com.ql.service.keshi.KeShiService;
import com.ql.service.zydj.ZuyuanyzService;
import com.ql.service.zydj.ZydjService;
import com.ql.utils.DateUtils;
import com.ql.utils.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class zydjController {
    @Resource
    private ZydjService zydjService;
    @Resource
    private ZuyuanyzService zuyuanyzService;
    @Resource
    private KeShiService keShiService;
    @Resource
    private BedService bedService;
    @Resource
    private DoctorService doctorService;
//住院病人查询
    @RequestMapping("/zycx")
    public String getzyxq(@RequestParam(value = "qryUserId",required = false)String qryUserId,
                          @RequestParam(value = "qryUserName",required = false)String qryUserName,
                          @RequestParam(value = "qryKeshi",required = false)String qryKeshi,
                          @RequestParam(value = "qrycheckInTime",required = false)String qrycheckInTime,
                          PageHelper ph, Model model) throws Exception{
        if(null == qryUserId || qryUserId.equals( "" )){
            qryUserId = "0";
        }
        if(null == qrycheckInTime || qrycheckInTime.equals( "" )){
            qrycheckInTime = "1971-12-12 12:12:12";
        }
        List<Zuyuandengji> zuyuandengjiList = zydjService.qryAllPatient( DateUtils.parse( qrycheckInTime ), Integer.parseInt( qryUserId ),qryUserName,qryKeshi,ph );

        model.addAttribute( "zuyuandengjiList", zuyuandengjiList );
        model.addAttribute( "qryUserId", qryUserId );
        model.addAttribute( "qryUserName", qryUserName );
        model.addAttribute( "qryKeshi", qryKeshi );
        model.addAttribute( "qrycheckInTime", qrycheckInTime );
        model.addAttribute( "currentPageNo", ph.getCurrentPageNo() );
        model.addAttribute( "totalCount", ph.getTotalCount() );
        model.addAttribute( "totalPageCount", ph.getTotalPageCount() );
        return "zhuyuanchaxun";
    }

    //跳转到查看病人详情
    @RequestMapping("/brck")
    public String getbrxq(@RequestParam("userId") String userId,
                          @RequestParam(value = "currentPageNo",required = false) String currentPageNo, Model model) {
        Zuyuandengji patient = zydjService.qryById( Integer.parseInt( userId ) );
        if (currentPageNo==null ){
            currentPageNo = "1";
        }
        PageHelper ph = new PageHelper();
        ph.setCurrentPageNo( Integer.parseInt( currentPageNo ));
        List<Zuyuanyz> zuyuanyzs = zuyuanyzService.qryAllYz( Integer.parseInt( userId ), ph );
        model.addAttribute( "patient", patient );
        model.addAttribute( "zuyuanyzs", zuyuanyzs );
        model.addAttribute( "currentPageNo", ph.getCurrentPageNo() );
        model.addAttribute( "totalCount", ph.getTotalCount() );
        model.addAttribute( "totalPageCount", ph.getTotalPageCount() );
        return "bingrenxiangqing";
    }

    //跳转到增加医嘱页面
    @RequestMapping("/tjyz/{userId}")
    public String addTjyz(@PathVariable String userId, Model model) {
        model.addAttribute( "userId", userId );
        return "zhuyuanyizhu";
    }
//添加医嘱
    @RequestMapping("/newyz")
    public String addNewYz(HttpSession session, String userId, String zDate,  String zsummary, Model model) throws Exception {
        Zuyuanyz zuyuanyz = new Zuyuanyz();
        Doctor doctor = (Doctor) session.getAttribute( "doctor" );
        zuyuanyz.setKId( doctor.getDocid() );
        zuyuanyz.setUserId( Integer.parseInt( userId ) );
        zuyuanyz.setZDate( DateUtils.parse( zDate ) );
//        SimpleDateFormat sdf = new SimpleDateFormat( "hh:mm" );
        zuyuanyz.setZTime( DateUtils.parse( zDate ) );
        zuyuanyz.setZsummary( zsummary );
        model.addAttribute( "userId", userId );
        boolean flag = zuyuanyzService.addYz( zuyuanyz );
        if (flag) {
            return "tjyzSuccess";
        } else {
            return "redirect:brck";
        }
    }

    //   跳转到医嘱确认执行页面
    @RequestMapping("/zxhl/{zId}")
    public String qurRenhl(@PathVariable String zId,
                           Model model) {
        Zuyuanyz zuyuanyz = zuyuanyzService.qryYZById( Integer.parseInt( zId ) );
        model.addAttribute( "zuyuanyz",zuyuanyz );
        return "huliqueren";
    }
//    跳转到住院登记页面
    @RequestMapping("/zydj")
    public String getZydj(Model model){
        List<Keshi> keshiList = keShiService.getAllKeShi();
        model.addAttribute( "keshiList",keshiList );

        return "zhuyuandengj";
    }
//    动态获取床位
    @RequestMapping("/bedList")
    @ResponseBody
    public String getBedList(@RequestParam(value = "kId",required = false) String kId,
                                Model model){
        List<Bed> bedList = bedService.qryByKid( Integer.parseInt( kId ) );
        model.addAttribute( "bedList",bedList );
        String bList=JSON.toJSON( bedList ).toString();
        return bList;
    }
//动态获取医生
    @RequestMapping(value = "/doctor",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getDoctorList(@RequestParam(value = "skId",required = false) String skId,
                                Model model){
        List<Doctor> doctorList = doctorService.qryByKid(Integer.parseInt( skId ) );
        model.addAttribute( "doctorList",doctorList );
        String bList=JSON.toJSON( doctorList ).toString();
        return bList;
    }
//验证住院号
    @RequestMapping(value = "/idConfirm/{userId}",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String CheckId(@PathVariable String userId){
        boolean flag = zydjService.qryId( Integer.parseInt( userId ) );
        if (flag){
            String msg = "{\"retMsg\":true}";
            return msg;
        }else{
            String msg = "{\"retMsg\":false}";
        return msg;
        }
    }

//跳转到病人转科页面
    @RequestMapping("/brzk")
    public String reBrzk(@RequestParam("userId") String userId,Model model){
        Zuyuandengji patient = zydjService.qryById( Integer.parseInt( userId ) );
        List<Keshi> keshiList = keShiService.getAllKeShi();
        model.addAttribute("patient",patient  );
        model.addAttribute( "keshiList",keshiList );
        return "bingrenzk";
    }
//   病人转科处理
    @RequestMapping("/zhuanke")
    @ResponseBody
    public String ZKsuccess(String userId,String kId,String dId,String bNo){
        Zuyuandengji zuyuandengji =  zydjService.qryById( Integer.parseInt( userId ) );
       /*获取原床位号*/
        int bNumber = zuyuandengji.getbNo();
        zuyuandengji.setkId( Integer.parseInt( kId ) );
        zuyuandengji.setDocId( Integer.parseInt( dId ) );
        zuyuandengji.setbNo( Integer.parseInt( bNo ) );
        boolean flag = zydjService.updKeshi( zuyuandengji );
        if (flag){
//            将原床位状态设置为空闲
            Bed bed1 = bedService.qryAllInfoByID( bNumber);
            bed1.setBState( 2 );
            bed1.setHNo( 0 );
            if (bedService.updBed( bed1 )){
                System.out.println("原床位更新成功");
            }else{
                System.out.println("原床位更新失败");
            }
//            设置新床位状态为使用中
            Bed bed = bedService.qryAllInfoByID( Integer.parseInt( bNo ) );
            bed.setBState( 1 );
            bed.setHNo( zuyuandengji.getUserId() );
            if (bedService.updBed( bed )){
                System.out.println("原床位更新成功");
            }else{
                System.out.println("原床位更新失败");
            }
            String msg = "{\"retMsg\":true}";
            return msg;
        }else{
            String msg = "{\"retMsg\":false}";
            return msg;
        }

    }

//住院登记
    @RequestMapping("/djsave")
    public String djSuccess(@RequestParam(value = "userId", required = false) String userId,
                            @RequestParam(value = "userName", required = false) String userName,
                            @RequestParam(value = "sex", required = false) String sex,
                            @RequestParam(value = "age", required = false) String age,
                            @RequestParam(value = "idCard", required = false) String idCard,
                            @RequestParam(value = "kId", required = false) String kId,
                            @RequestParam(value = "bNo", required = false) String bNo,
                            @RequestParam(value = "address", required = false) String address,
                            @RequestParam(value = "docId", required = false) String docId,
                            @RequestParam(value = "checkInTime", required = false) String checkInTime,
                            @RequestParam(value = "summary", required = false) String summary,
                            @RequestParam(value = "contacts", required = false) String contacts,
                            @RequestParam(value = "iphone", required = false) String iphone
    ) throws Exception {
        if (userId.equals( "" )) {
            userId = "0";
        }
        if (sex.equals( "" )) {
            sex = "0";
        }
        if (age.equals( "" )) {
            age = "0";
        }
        if (kId.equals( "" )) {
            kId = "0";
        }
        if (bNo.equals( "" )) {
            bNo = "0";
        }
        if (docId.equals( "" )) {
            docId = "0";
        }

        Zuyuandengji patient = new Zuyuandengji();
        Bed bed = bedService.qryAllInfoByID( Integer.parseInt( bNo ) );
        patient.setUserId( Integer.parseInt( userId ) );
        patient.setUserName( userName );
        patient.setSex( Integer.parseInt( sex ) );
        patient.setAge( Integer.parseInt( age ) );
        patient.setkId( Integer.parseInt( kId ) );
        patient.setbNo( Integer.parseInt( bNo ) );
        patient.setDocId( Integer.parseInt( docId ) );
        patient.setAddress( address );
        patient.setIdCard( idCard );
        patient.setSummary( summary );
        patient.setContacts( contacts );
        patient.setIphone( iphone );
        patient.setCheckInTime( DateUtils.parse( checkInTime ) );
        if (zydjService.addPatient( patient )) {
            bed.setHNo( Integer.parseInt( userId ) );
            bed.setBState( 1 );
            boolean flag = bedService.updBed( bed );
            if (flag){
                System.out.println("更新床位成功");
            }else {
                System.out.println("更新床位信息失败");
            }
            System.out.println( "登记成功" );
            return "zydjcg";
        } else {
            System.out.println( "登记失败" );
        }
        return "zhuyuandengj";
    }
//住院护理分页
    @RequestMapping("/zyhl")
    public String getUnYiZ(@RequestParam(value = "qryUserId",required = false)String qryUserId,
                           @RequestParam(value = "qryUserName",required = false)String qryUserName,
                           @RequestParam(value = "qryKeshi",required = false)String qryKeshi,
                           PageHelper ph, Model model) {
        if(null == qryUserId || qryUserId.equals( "" )){
            qryUserId = "0";
        }
        List<Zuyuanyz> zuyuanyzs = zuyuanyzService.qryAllUn( Integer.parseInt( qryUserId ),qryUserName,qryKeshi,ph );

        model.addAttribute( "zuyuanyzs", zuyuanyzs );
        model.addAttribute( "qryUserId", qryUserId );
        model.addAttribute( "qryUserName", qryUserName );
        model.addAttribute( "qryKeshi", qryKeshi );
        model.addAttribute( "currentPageNo", ph.getCurrentPageNo() );
        model.addAttribute( "totalCount", ph.getTotalCount() );
        model.addAttribute( "totalPageCount", ph.getTotalPageCount() );
        return "zhuyuanhuli";
    }
//执行护理任务
    @RequestMapping("/doYZ")
    public String goForYz(String zId,String doTime,HttpSession session,Model model)throws Exception{
    boolean flag = false;
    Doctor doctor = (Doctor) session.getAttribute( "doctor" );
    Zuyuanyz zuyuanyz = zuyuanyzService.qryYZById( Integer.parseInt( zId  ));
    zuyuanyz.setDKid( doctor.getDocid() );
    zuyuanyz.setDoTime( DateUtils.parse( doTime ) );
    flag = zuyuanyzService.updateYZ( zuyuanyz );
    if (flag){
        return "zxyzSuccess";
    }else {
        return "zxyzFailed";
    }
    }

//跳转到出院登记页面
    @RequestMapping("/cydj")
    public String getCydj(String userId,Model model) {
        Zuyuandengji zuyuandengji = zydjService.qryById( Integer.parseInt( userId ) );
        model.addAttribute( "zuyuandengji",zuyuandengji );
        return "bingrenchuyuan";
    }

//    病人出院
    @RequestMapping("/chuyuan")
    @ResponseBody
    private String outHospital(String checkOutTime,String userId,String outsumary,String outyz)throws Exception{
        Zuyuandengji zuyuandengji = zydjService.qryById( Integer.parseInt( userId ) );
        zuyuandengji.setCheckOutTime( DateUtils.parse( checkOutTime ) );
        zuyuandengji.setOutsumary( outsumary );
        zuyuandengji.setOutyz( outyz );
        Bed bed = bedService.qryByID( zuyuandengji.getbNo() );
        boolean flag = zydjService.outHospital( zuyuandengji );
        if (flag){
            bed.setHNo( 0 );
            bed.setBState( 2 );
            boolean flag1 = bedService.updBed( bed );
            if (flag1){
                System.out.println("更新床位成功");
            }else {
                System.out.println("更新床位信息失败");
            }
            String msg = "{\"retMsg\":true}";
            return msg;
        }else{
            String msg = "{\"retMsg\":false}";
            return msg;
        }
    }

//   查询所有床位
    @RequestMapping("/bedlist")
    public String getAllBed(@RequestParam(value = "currentPageNo",required = false) String currentPageNo,
                            @RequestParam(value = "qrybNo",required = false) String qrybNo,
                            @RequestParam(value = "qryStatent",required = false) String qryStatent,
                            @RequestParam(value = "qryKName",required = false) String qryKName,
                            Model model){
        if (currentPageNo==null ){
            currentPageNo = "1";
        }
        if (qrybNo==null ||qrybNo.equals( "" ) ){
            qrybNo = "0";
        }
        if (qryStatent==null ||qryStatent.equals( "" ) ){
            qryStatent = "0";
        }
        PageHelper ph = new PageHelper();
        ph.setCurrentPageNo( Integer.parseInt( currentPageNo ));
        List<Bed> bedList= bedService.qryAllBed( ph, qryKName,Integer.parseInt(qrybNo),Integer.parseInt(qryStatent ));
        model.addAttribute( "bedList",bedList );
        model.addAttribute( "currentPageNo", ph.getCurrentPageNo() );
        model.addAttribute( "totalCount", ph.getTotalCount() );
        model.addAttribute( "totalPageCount", ph.getTotalPageCount() );
        model.addAttribute( "qryKName",qryKName );
        model.addAttribute( "qrybNo", qrybNo);
        model.addAttribute( "qryStatent", qryStatent);
        return "beds";
    }
//    由床位ID查找床位信息
    @RequestMapping("/bedInfo")
    public String qryByID(String bNo,Model model){
        Bed bed = bedService.qryAllInfoByID( Integer.parseInt( bNo ) );
        if (bed.getBState()==1){
            bed = bedService.qryByID( Integer.parseInt( bNo ) );
            model.addAttribute( "bed",bed );
        }else {
            model.addAttribute( "bed",bed );
        }
        return "bedMsg";
    }

//   跳转到增加床位信息页面
    @RequestMapping("bedAdd")
    public String GoBedAdd(Model model){
        List<Keshi> keshiList = keShiService.getAllKeShi();
        model.addAttribute( "keshiList", keshiList);
        return "bedAdd";
    }
// 增加床位
    @RequestMapping("bedAddInfo")
    public String addSuccess(@ModelAttribute("bed")Bed bed,Model model){
        if (bedService.addBed( bed )){
            return "bedtjSuccess";
        }else {
            return "bedtjFiled";
        }
    }

    //床位号验证
    @RequestMapping(value = "/bedNo",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getDoctorList(String bNo){
        Bed bed = bedService.qryAllInfoByID(Integer.parseInt( bNo ) );
        if (bed==null){
            String msg = "{\"retMsg\":true}";
            return msg;
        }else {
            String msg = "{\"retMsg\":false}";
            return msg;
        }
    }

//    删除床位
    @RequestMapping("delBed")
    @ResponseBody
    public String delBedById(String bNo){
        if (bedService.delBed( Integer.parseInt( bNo ) )){
            String msg = "{\"retMsg\":true}";
            return msg;
        }else {
            String msg = "{\"retMsg\":false}";
            return msg;
        }
    }

}
